package com.kits.store.ui.kits

import android.content.ContentUris
import android.content.ContentValues
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.os.Build
import android.provider.MediaStore
import androidx.annotation.RequiresApi
import java.io.ByteArrayOutputStream

class AudioStorage {

    /**
     * 存储音乐文件到标准文件夹
     */
    fun standFile(context:Context){
        /**
         * 从assets文件中读取音频文件
         */
        val assetsByteArray = readAsset2Byte(context,"testMusic.mp3")
        // 通过Media 存储到 标准文件夹中
        val contentValues = ContentValues()
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME,"first.mp3")
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE,"audio/x-mpeg")
        val insertUri = context.contentResolver?.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,contentValues)
        println("insertUri == $insertUri")
        insertUri?.let {
            context.contentResolver?.openOutputStream(it)
        }?.let {
            it.write(assetsByteArray)
            it.close()
        }
    }

    fun queryFile(context: Context,name:String){
        val queryCursor = context.contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,
            MediaStore.MediaColumns.DISPLAY_NAME+"=?", arrayOf(name),null)
        println("queryUri == $queryCursor")
        queryCursor?.let {
            println("number == ${it.count}")
            if (it.count == 0){
                return
            }
            println("columnCount == ${it.columnCount}")

            it.moveToFirst()
            do {
                for (index in 0 until it.columnCount){
                    println("${it.getColumnName(index)} == ${it.getString(index)}")
                }
            } while (it.moveToNext())
        }
    }

    @RequiresApi(Build.VERSION_CODES.R)
    fun updateFile(context: Context, name:String){
        var mediaId: Long = -1
        val queryCursor = context.contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,
            MediaStore.MediaColumns.DISPLAY_NAME+"=?", arrayOf(name),null)
        println("queryUri == $queryCursor")
        queryCursor?.let {
            println("number == ${it.count}")
            if (it.count == 0){
                return
            }
            println("columnCount == ${it.columnCount}")

            it.moveToFirst()
            mediaId = it.getLong(it.getColumnIndex(MediaStore.Images.Media._ID))
            do {
                for (index in 0 until it.columnCount){
                    println("${it.getColumnName(index)} == ${it.getString(index)}")
                }
            } while (it.moveToNext())
        }
        println("开始更新----")
        // 更新
        val contentValues = ContentValues()
        contentValues.put(MediaStore.MediaColumns.AUTHOR,"张三")
        val updateUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,mediaId)
        val ret = context.contentResolver?.update(updateUri,contentValues,null,null)
        println("更新结果:: ret == $ret")

    }

    private fun readAsset2Byte(context:Context, file:String):ByteArray{
        val inputStream = context.resources.assets.open(file)
        val out = ByteArrayOutputStream()
        inputStream.use { inputStream ->
            val bytes = ByteArray(1024 * 5)
            var len = inputStream.read(bytes)
            while ((len) != -1) {
                out.write(bytes, 0, len)
                len = inputStream.read(bytes)
            }
        }
        val tmp = out.toByteArray()
        out.close()
        return tmp
    }
}